public class defaultBoard implements Board {

	private Chess chess[][] = new Chess[8][8];
	private Game myGame;
	private Adrs tempAdrs;
	private History myHistoy=new defaultHistory();
	private BoardMaker myMaker;
	//private Boolean hascheck=false;

	public defaultBoard(Game g) {
		this.myGame = g;
		this.myMaker=new defaultChessCreater();
	}
	public defaultBoard(Game g,BoardMaker m){
		this.myGame=g;
		this.myMaker=m;
		this.chess=this.myMaker.makeChess();
	}

	@Override
	public Boolean hasGameEnd() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (this.chess[i][j] != null
						&& this.chess[i][j].getTeam() == (this.myGame
								.getCurrentPlayer())) {
					
					
					
					AdrsList temp = this.chess[i][j].getMovement(new defaultAdrs(i, j), this.getmap());
					for (int p = 0; p < temp.getPathNum(); p++) {
						while (temp.getAdrsNum(p) > 0) {
							//System.out.println(this.myGame.getCurrentPlayer());
							return false;
						}
					}
				}
			}
		}
		return true;
	}

	@Override
	public AdrsList showMovement(Adrs adrs) {
		// TODO Auto-generated method stub
		this.setTempAdrs(adrs);
		return chess[adrs.getAdrs(0)][adrs.getAdrs(1)].getMovement(adrs,
				new defaultROBoard(this.chess, this));
	}

	@Override
	public void moveChase(Adrs st, Adrs ed) {
		// TODO Auto-generated method stub
		this.chess[st.getAdrs(0)][st.getAdrs(1)].move(ed, this);
	}

	@Override
	public void move(Adrs st, Adrs ed) {
		// TODO Auto-generated method stub
		if (chess[st.getAdrs(0)][st.getAdrs(1)] != null) {
			chess[ed.getAdrs(0)][ed.getAdrs(1)] = chess[st.getAdrs(0)][st
					.getAdrs(1)];
			chess[st.getAdrs(0)][st.getAdrs(1)] = null;
		} else {
			System.out.println("start:" + st + "-goal:" + ed + "-error");
		}
		this.myGame.drawNewBoard();
		//System.out.println(st + "," + ed);

	}

	@Override
	public History getHistory() {
		// TODO Auto-generated method stub
		return this.myHistoy;
	}

	@Override
	public ROBoard getmap() {
		// TODO Auto-generated method stub
		return new defaultROBoard(this.chess, this);
	}

	public void setTempAdrs(Adrs a) {
		this.tempAdrs = a;
	}

	public Adrs getTempAdrs(Adrs a) {
		return this.tempAdrs;
	}

	@Override
	public boolean hasCheck() {
		defaultAdrs king = null;
		// find my King
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (chess[i][j] != null
						&& chess[i][j].getPost() == 0
						&& this.chess[i][j].getTeam() == this.myGame
								.getCurrentPlayer()) {
					king = new defaultAdrs(i, j);
				}
			}
		}
		if (king == null)
			System.out.println("cant find King");
		// check
		else {
			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++) {
					if (this.chess[i][j] != null
							&& this.chess[i][j].getTeam() == (this.myGame
									.getCurrentPlayer()+1)%2) {
						//System.out.println(chess[i][j].getTeam());
						AdrsList temp = chess[i][j].showMovement(new defaultAdrs(i, j), this.getmap());
						for (int a = 0; a < temp.getPathNum(); a++) {
							while (temp.getAdrsNum(a) > 0) {
								if (king.equals(temp.getFirstAdrs(a))) {
									return true;
								}
							}
						}
					}
				}
			}
		}
		return false;
	}

	@Override
	public boolean hasCheck(Adrs st, Adrs ed) {
		// TODO Auto-generated method stub
		// try
		// System.out.println(st+":"+ed);
		boolean tcheck = false;
		Chess Ctemp = this.chess[ed.getAdrs(0)][ed.getAdrs(1)];
		this.chess[ed.getAdrs(0)][ed.getAdrs(1)] = this.chess[st.getAdrs(0)][st
				.getAdrs(1)];
		this.chess[st.getAdrs(0)][st.getAdrs(1)] = null;

		defaultAdrs king = null;
		// find enemy King
		for (int i = 0; i < this.getBoardSize(0, 0); i++) {
			for (int j = 0; j < this.getBoardSize(1, i); j++) {
				if (chess[i][j] != null
						&& chess[i][j].getPost() == 0
						&& this.chess[i][j].getTeam() == (this.myGame
								.getCurrentPlayer())) {
					king = new defaultAdrs(i, j);
				}
			}
		}
		if (king == null)
			System.out.println("cant find King");
		// check
		else {
			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++) {
					if (this.chess[i][j] != null
							&& this.chess[i][j].getTeam() == (this.myGame
									.getCurrentPlayer() + 1) % 2) {
						//System.out.print(i+":"+j+"_");
						AdrsList temp = chess[i][j].showMovement(
								new defaultAdrs(i, j), this.getmap());
						for (int a = 0; a < temp.getPathNum(); a++) {
							while (temp.getAdrsNum(a) > 0) {
								//System.out.println(temp.lookFirstAdrs(a));
								if (king.equals(temp.getFirstAdrs(a))) {
									this.chess[st.getAdrs(0)][st.getAdrs(1)] = this.chess[ed
											.getAdrs(0)][ed.getAdrs(1)];
									this.chess[ed.getAdrs(0)][ed.getAdrs(1)] = Ctemp;
									return true;
								}
							}
						}
						
					}
	
					
				}
			}
			 
		}
		this.chess[st.getAdrs(0)][st.getAdrs(1)] = this.chess[ed.getAdrs(0)][ed
				.getAdrs(1)];
		this.chess[ed.getAdrs(0)][ed.getAdrs(1)] = Ctemp;
		return tcheck;
	}

	@Override
	public int callCurrentPlayer(String message, String title, int optionType,
			int messageType, Object[] options, Object initialValue) {
		// TODO Auto-generated method stub
		return this.myGame.callCurrentPlayer(message, title, optionType,
				messageType, options, initialValue);
	}

	@Override
	public int callNextPlayer(String message, String title, int optionType,
			int messageType, Object[] options, Object initialValue) {
		// TODO Auto-generated method stub
		return this.myGame.callNextPlayer(message, title, optionType,
				messageType, options, initialValue);
	}

	@Override
	public void setChess(Adrs adrs, Chess c) {
		// TODO Auto-generated method stub
		this.chess[adrs.getAdrs(0)][adrs.getAdrs(1)] = c;
		this.myGame.drawNewBoard();

	}

	@Override
	public int getBoardSize(int d, int line) {
		if(d==0){
			return this.chess.length;
		}else if(d==1&&line<8){
			return this.chess[line].length;
		}else{
			//System.out.println("Boadr is 8*8.you input:"+line);
			return -1;
		}
	}
	@Override
	public int getHistoryNum() {
		// TODO Auto-generated method stub
		return this.myHistoy.getHistoryNum();
	}
	@Override
	public int getHistoryPost(int num) {
		// TODO Auto-generated method stub
		return this.myHistoy.getPost(num);
	}
	@Override
	public Adrs getHistorySt(int num) {
		// TODO Auto-generated method stub
		return this.myHistoy.getSt(num);
	}
	@Override
	public Adrs getHistoryEd(int num) {
		// TODO Auto-generated method stub
		return this.myHistoy.getSt(num);
	}
	@Override
	public String getHistoryNote(int num) {
		// TODO Auto-generated method stub
		return this.myHistoy.getNote(num);
	}
	@Override
	public void killChess(Adrs adrs) {
		this.chess[adrs.getAdrs(0)][adrs.getAdrs(1)]=null;
		
	}
	@Override
	public void resetBoard() {
		this.chess=this.myMaker.makeChess();
	}

}
